/**
 * Created by Eoly on 2016/12/31.
 */
function Player(touchCanvas) {

    this.id = 0;

    this.mark = 0;

    this.touch = new PlayerTouch();
    this.plane = null;
    this.playerCanvas = touchCanvas;

    //需外部设置
    this.bulletCanvas = null;

    this.init = function () {
        this.touch.canvas = touchCanvas;
        this.touch.openTouch();
    };

    this.init();

    this.startGame = function () {

        enemy = new Enemy();
        enemy.waveStart();
        enemy.enemyCanvas = planeCanvas;


        this.mark = 0;

        this.plane = new Plane();
        this.plane.setPlayerBelongs(this);
        this.plane.setCentrePoint(new Point(window.innerWidth * 0.5, window.innerHeight * 0.9));
        this.plane.setPlaneCanvas(this.playerCanvas);
        this.plane.setBulletCanvas(this.bulletCanvas);
        this.plane.draw();
        this.plane.startShoot();

        this.writeInfo();

        this.touch.plane = this.plane;
    };

    this.overGame = function () {
        var _this = this;

        alert("游戏结束，你的得分是" + this.mark + "，游戏即将重新开始");

        this.overGameAll();

        setTimeout(function () {
            _this.startGame();
        }, 2000);


    };

    this.overGameAll = function () {
        var _this = this;
        this.plane.endOfShoot();
        this.plane.isDeath = true;
        this.plane.isDestroied = true;
        this.playerCanvas.ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
        this.plane = null;
        this.touch.plane = null;
        clearInterval(enemy.wavingHandle);
    };

    this.getNumWithPercentAndMaxNum = function (percent, maxNum) {
        return parseInt(maxNum * percent);
    };

    this.writeInfo = function () {
        if (this.plane != null) {
            var wWidth = window.innerWidth;
            var wHeight = window.innerHeight;
            var ctx = infoCanvas.ctx;
            infoCanvas.clearAll();

            var mark = this.mark;
            var mainClip = this.plane.mainWeaponPack != null && this.plane.mainWeaponPack.clip < 10000 ? this.plane.mainWeaponPack.clip : "无限";
            var secondaryClip = this.plane.secondaryWeaponPack != null ? this.plane.secondaryWeaponPack.clip : "无副武器";
            var HP = this.plane.HP;
            var maxHP = this.plane.maxHP;
            var difficulty = enemy.difficulty;
            // var info = document.getElementsByClassName("info")[0];
            // info.innerHTML = "<p><span class='fl'>生命值:" + HP+"/"+maxHP + "</span><span class='fr'>积分:" + mark + "</span></p>" +
            //     "<p><span class='fl'>难度:" + difficulty + "</span></p>" +
            //     "<p><span class='fl'>主武器弹药:" + mainClip + "</span><span class='fr'>副武器弹药:" + secondaryClip + "</span></p>"

            //画生命值边框
            var pointHP = new Point(55, 10);
            ctx.beginPath();
            ctx.rect(pointHP.x, pointHP.y, 80, 14);
            ctx.strokeStyle = "white";
            ctx.stroke();
            // ctx.closePath();

            //画阴影
            ctx.save();
            ctx.shadowOffsetX = 2; // 阴影Y轴偏移
            ctx.shadowOffsetY = 2; // 阴影X轴偏移
            ctx.shadowBlur = 3; // 模糊尺寸
            ctx.shadowColor = 'rgba(0, 0, 0, 0.5)'; // 颜色

            //画生命值填充
            var percent = HP / maxHP;
            var color = "rgba(48,144,144,0.8)";
            if (percent < 0.66) color = "rgba(144,144,48,0.8)";
            if (percent < 0.33) color = "rgba(144,48,48,0.8)";
            ctx.beginPath();
            ctx.rect(pointHP.x, pointHP.y, this.getNumWithPercentAndMaxNum(HP / maxHP, 80), 14);
            ctx.fillStyle = color;
            ctx.fill();
            // ctx.closePath();

            //画生命值提示文字
            ctx.beginPath();
            ctx.font = "12px";
            // ctx.textAlign = "center";
            ctx.fillStyle = "#fff";
            ctx.fillText("生命值：", pointHP.x - 48, pointHP.y + 11, 70);
            // ctx.closePath();

            //画生命值文字
            ctx.beginPath();
            ctx.font = "12px";
            // ctx.textAlign = "center";
            ctx.fillStyle = "#000";
            ctx.fillText(HP + "/" + maxHP, pointHP.x + 10, pointHP.y + 12, 70);
            // ctx.closePath();



            var croePoint = new Point(wWidth-55,10);

            //画得分提示文字
            ctx.beginPath();
            ctx.font = "12px";
            // ctx.textAlign = "center";
            ctx.fillStyle = "#fff";
            ctx.fillText("当前难度：  "+difficulty+" 得分：", croePoint.x - 120, croePoint.y + 11, 200);
            // ctx.closePath();

            //画得分
            ctx.beginPath();
            ctx.font = "12px";
            // ctx.textAlign = "center";
            ctx.fillStyle = "#fff";
            ctx.fillText(mark+"", croePoint.x + 10, croePoint.y + 12, 33);
            // ctx.closePath();


            var clipPoint = new Point(wWidth-55,wHeight-30);

            //画武器提示文字
            ctx.beginPath();
            ctx.font = "12px";
            // ctx.textAlign = "center";
            ctx.fillStyle = "#fff";
            ctx.fillText("主武器："+mainClip+"      副武器："+secondaryClip, clipPoint.x - 160 , clipPoint.y + 11, 250);
            // ctx.closePath();




        }
    }
}